/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 *
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.core.command.invoker.processor;

import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

import org.junit.Test;
import org.openinsula.vulcano.core.command.Command;
import org.openinsula.vulcano.core.command.mock.SubtypeOfTransactionalOnDeclarationDICommand;
import org.openinsula.vulcano.core.command.mock.SubtypeOfTransactionalOnExecuteDICommand;
import org.openinsula.vulcano.core.command.mock.TransactionalOnDeclarationDICommand;
import org.openinsula.vulcano.core.command.mock.TransactionalOnExecuteDICommand;
import org.openinsula.vulcano.core.command.processor.CommandChain;
import org.openinsula.vulcano.core.command.processor.TransactionalCommandProcessor;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class TransactionalCommandProcessorTestCase {

	@Test
	public void testTransactionDefinitionWithDefaultValues() {
		TransactionalCommandProcessor commandProcessor = new TransactionalCommandProcessor() {
			@Override
			protected TransactionDefinition getTransactionDefinition(Command<?> command) {
				TransactionDefinition transactionDefinition = super.getTransactionDefinition(command);

				assertEquals(TransactionDefinition.PROPAGATION_REQUIRED, transactionDefinition.getPropagationBehavior());
				assertEquals(TransactionDefinition.ISOLATION_DEFAULT, transactionDefinition.getIsolationLevel());

				return transactionDefinition;
			}
		};

		CommandChain commandChain = createMockCommandChain();

		commandProcessor.setTransactionManager(createMockTransactionManager());
		commandProcessor.doExecute(new TransactionalOnDeclarationDICommand(), commandChain);

		commandProcessor.setTransactionManager(createMockTransactionManager());
		commandProcessor.doExecute(new SubtypeOfTransactionalOnDeclarationDICommand(), commandChain);

		commandProcessor.setTransactionManager(createMockTransactionManager());
		commandProcessor.doExecute(new TransactionalOnExecuteDICommand(), commandChain);

		commandProcessor.setTransactionManager(createMockTransactionManager());
		commandProcessor.doExecute(new SubtypeOfTransactionalOnExecuteDICommand(), commandChain);
	}

	@Test
	public void testTransactionDefinitionWithCustomValues() {
		TransactionalCommandProcessor commandProcessor = new TransactionalCommandProcessor() {
			@Override
			protected TransactionDefinition getTransactionDefinition(Command<?> command) {
				TransactionDefinition transactionDefinition = super.getTransactionDefinition(command);

				assertEquals(TransactionDefinition.PROPAGATION_MANDATORY, transactionDefinition
						.getPropagationBehavior());
				assertEquals(TransactionDefinition.ISOLATION_SERIALIZABLE, transactionDefinition.getIsolationLevel());

				return transactionDefinition;
			}
		};

		CommandChain commandChain = createMockCommandChain();

		commandProcessor.setTransactionManager(createMockTransactionManager());
		commandProcessor.doExecute(new TransactionalOnDeclarationDICommand() {
			private static final long serialVersionUID = 1L;

			@Override
			@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.MANDATORY)
			public Boolean execute() {
				return super.execute();
			}

		}, commandChain);
	}

	@Test
	public void testNullTransactionDefinition() {
		TransactionalCommandProcessor commandProcessor = new TransactionalCommandProcessor() {
			@Override
			protected TransactionDefinition getTransactionDefinition(Command<?> command) {
				TransactionDefinition transactionDefinition = super.getTransactionDefinition(command);

				assertEquals(TransactionDefinition.PROPAGATION_REQUIRED, transactionDefinition.getPropagationBehavior());
				assertEquals(TransactionDefinition.ISOLATION_DEFAULT, transactionDefinition.getIsolationLevel());

				return transactionDefinition;
			}
		};

		CommandChain commandChain = createMockCommandChain();
		commandProcessor.setTransactionManager(createMockTransactionManager());

		commandProcessor.doExecute(new Command<Object>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Object execute() {
				return null;
			}
		}, commandChain);
	}

	@Test(expected = RuntimeException.class)
	public void testRollbackCommand() {
		TransactionalCommandProcessor commandProcessor = new TransactionalCommandProcessor();
		CommandChain commandChain = createMockCommandChain();

		commandProcessor.setTransactionManager(createRollbackMockTransactionManager());

		Command<Boolean> readOnlyCommand = new Command<Boolean>() {
			private static final long serialVersionUID = 1L;

			@Transactional(readOnly = true)
			@Override
			public Boolean execute() {
				throw new RuntimeException();
			}

		};

		commandProcessor.doExecute(readOnlyCommand, commandChain);
	}

	private CommandChain createMockCommandChain() {
		CommandChain commandChain = new CommandChain() {
			@Override
			public <T> T doChain(Command<T> command) {
				return command.execute();
			}
		};
		return commandChain;
	}

	private PlatformTransactionManager createMockTransactionManager() {
		PlatformTransactionManager transactionManager = createMock(PlatformTransactionManager.class);
		expect(transactionManager.getTransaction((TransactionDefinition) anyObject())).andReturn(
				createNiceMock(TransactionStatus.class));
		transactionManager.commit((TransactionStatus) anyObject());
		replay(transactionManager);
		return transactionManager;
	}

	private PlatformTransactionManager createRollbackMockTransactionManager() {
		PlatformTransactionManager transactionManager = createMock(PlatformTransactionManager.class);
		expect(transactionManager.getTransaction((TransactionDefinition) anyObject())).andReturn(
				createNiceMock(TransactionStatus.class));
		transactionManager.commit((TransactionStatus) anyObject());
		transactionManager.rollback((TransactionStatus) anyObject());
		replay(transactionManager);
		return transactionManager;
	}

}